<?php
/**
 * Абстрактный класс таблицы БД для данного модуля.
 */
abstract class Users_Table_Abstract extends Workset_Db_Table {
	// Данное поле должно возращаться в виде "unix timestamp", а не в виде строки.
	const FIELD_AS_TIMESTAMP = 1;

	/**
	 * @var array
	 */
	protected $_aMySqlFieldConverter;

	/**
	 * Инициализация объекта класса.
	 * 
	 * @param array $config
	 */
	public function __construct($config = array()) {
		parent::__construct($config);

		if (!is_array($this->_aMySqlFieldConverter)) {
			$this->_aMySqlFieldConverter = array();
		}

		foreach ($this->_aMySqlFieldConverter as $sField => $mValue) {
			switch ($mValue) {
				case self::FIELD_AS_TIMESTAMP:
					$sTable = $this->getTableName();
					$mValue = new Zend_Db_Expr("UNIX_TIMESTAMP(`$sTable`.`$sField`)");
					break;

				default:
					unset($this->_aMySqlFieldConverter[$sField]);
					continue 2;
			}

			$this->_aMySqlFieldConverter[$sField] = $mValue;
		}
	}

	/**
	 * Получение названия таблицы.
	 * 
	 * @return string
	 */
	public function getTableName() {
		return $this->_name;
	}

	/**
	 * (non-PHPdoc)
	 * @see library/Zend/Db/Table/Zend_Db_Table_Abstract#select()
	 */
	public function select($withFromPart = self::SELECT_WITHOUT_FROM_PART) {
		$oSelect = parent::select($withFromPart);

		try {
			$aFields = array_merge(array_combine(array_keys($this->_metadata),array_keys($this->_metadata)),$this->_aMySqlFieldConverter);

			if ($withFromPart == self::SELECT_WITHOUT_FROM_PART) {
				$oSelect->from($this->getTableName(),$aFields);
			}
			else {
				$oSelect->reset(Zend_Db_Select::COLUMNS);
				$oSelect->columns($aFields);
			}
		}
		catch (Exception $oException) {
			die($oException->getMessage());
		}

		return $oSelect;
	}
}